/**
 * @file SDPHandler.hpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief SDP工具
 * @version 0.0.1
 * @date 2020-07-18
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */
#pragma once
#include <string>
#include <map>
#include "STL_Log.hpp"

class SDPHandler
{
public:
    enum media_operate_type_e
    {
        MOT_UNKNOWN = 0,
        MOT_PLAY,
        MOT_DOWNLOAD,
        MOT_PLAYBACK,
        MOT_TALK,
    };

    struct sdp_description_t
    {
        std::string v_version;

        ///< o=34020000002000000001 B8975BC37429408E83BBAB32535D07B2 0 IN IP4 172.18.3.203
        std::string o_username; ///< owner/creator
        std::string o_sessid;
        std::string o_sess_version;
        std::string o_nettype;
        std::string o_addrtype;
        std::string o_addr;

        ///< s=Play
        std::string s_sess_name; ///< media_operate_type_e

        ///< u=34020000001320000002
        std::string u_uri;

        ///< c=IN IP4 172.18.3.204
        std::string c_nettype;
        std::string c_addrtype;
        std::string c_addr;

        ///< t= 0 0
        std::string t_startime;
        std::string t_endtime;

        ///< m=video 38930 RTP/AVP 96 98 97
        std::string m_media; ///< media type
        std::string m_port;
        std::string m_tmode;       ///< TCP(TCP/RTP/AVP) or UDP(RTP/AVP) media transmit mode
        std::string m_payloadtype; ///< payload type

        ///< a=recvonly
        ///< a=rtpmap:96 PS/90000
        std::map<std::string, std::string> a_attrimap; ///< 流列表

        ///< y=0200003000
        std::string y_ssrc;

        ///< f=v/1/5/3///a///
        std::string f_media_desc;

        void print()
        {
            stl::debug("o_username: %s", o_username.c_str());
            stl::debug("o_sessid: %s", o_sessid.c_str());
            stl::debug("o_sess_version: %s", o_sess_version.c_str());
            stl::debug("o_nettype: %s", o_nettype.c_str());
            stl::debug("o_addrtype: %s", o_addrtype.c_str());
            stl::debug("o_addr: %s", o_addr.c_str());
            stl::debug("s_sess_name: %s", s_sess_name.c_str());
            stl::debug("u_uri: %s", u_uri.c_str());
            stl::debug("c_nettype: %s", c_nettype.c_str());
            stl::debug("c_addrtype: %s", c_addrtype.c_str());
            stl::debug("c_addr: %s", c_addr.c_str());
            stl::debug("t_startime: %s", t_startime.c_str());
            stl::debug("t_endtime: %s", t_endtime.c_str());
            stl::debug("m_media: %s", m_media.c_str());
            stl::debug("m_port: %s", m_port.c_str());
            stl::debug("m_tmode: %s", m_tmode.c_str());
            stl::debug("m_payloadtype: %s", m_payloadtype.c_str());
            stl::debug("y_ssrc: %s", y_ssrc.c_str());
            stl::debug("f_media_desc: %s", f_media_desc.c_str());

            for (auto a : a_attrimap)
            {
                stl::debug("attri key %s, value %s", a.first.c_str(), a.second.c_str());
            }
        }
    };
    static int ParseSdp(const char *buf, SDPHandler::sdp_description_t &sdp_desc);
    static int AssembleSdp(std::string &sdp);
    static std::string generateSDP(std::string username,std::string rtpIp,uint16_t rtpPort,std::string ssrc);
};
